﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using Microsoft.Office.Interop.Word;
using System.Reflection;

namespace common
{
    public class ExportWord
    {
        public void ExportToWord(System.Windows.Forms.DataGridView agrid)
        {
            Object none = System.Reflection.Missing.Value;
            Microsoft.Office.Interop.Word.Application wordApp = new Microsoft.Office.Interop.Word.Application();
            wordApp.Visible = false;
            Microsoft.Office.Interop.Word.Document document = wordApp.Documents.Add(ref none, ref none, ref none, ref none);

            System.Windows.Forms.ProgressBar abar = new System.Windows.Forms.ProgressBar();
            abar.Width = agrid.Width / 3;
            abar.Height = 16;
            abar.Minimum = 0;
            abar.Maximum = agrid.RowCount;
            abar.Step = 1;
            abar.Left = (agrid.Width - abar.Width) / 2;
            abar.Top = (agrid.Height - abar.Height) / 2;
            abar.Value = 0;

            abar.Parent = agrid;
            agrid.Controls.Add(abar);

            abar.Visible = true;

            System.Windows.Forms.Label alb = new System.Windows.Forms.Label();
            alb.Parent = abar;
            abar.Controls.Add(alb);
            alb.Left = abar.Width / 2;
            alb.Height = 10;
            alb.Top = 3;
            alb.AutoSize = true;
            alb.Text = "0%";
            alb.BackColor = System.Drawing.Color.FromArgb(100, abar.ForeColor);

            //建立表格
            Microsoft.Office.Interop.Word.Table table = document.Tables.Add(document.Paragraphs.Last.Range, agrid.Rows.Count * agrid.Columns.Count, 2, ref none, ref none);
            //设置边框
            table.Borders.Enable = 1;
            try
            {
                ////设置标题
                //for (int i = 0; i < agrid.Columns.Count; i++)
                //{
                //    table.Cell(0, i + 1).Range.Text = agrid.Columns[i].HeaderText;
                //}
                ////填充数据
                //for (int i = 1; i < agrid.Rows.Count; i++)
                //{
                //    for (int j = 0; j < agrid.Columns.Count; j++)
                //    {
                //        table.Cell(i + 1, j + 1).Range.Text = (agrid[j, i - 1].Value == null) ? "" : agrid[j, i - 1].Value.ToString();
                //    }
                //    abar.Value = abar.Value + 1;
                //    alb.Text = abar.Value * 100 / abar.Maximum + "%";
                //}
                for (int row = 0; row < agrid.Rows.Count; row++)
                {
                    for (int column = 0; column < agrid.Columns.Count; column++)
                    {
                        //设置标题
                        table.Cell(row * agrid.Columns.Count + column + 1, 1).Range.Text = agrid.Columns[column].HeaderText;
                        //填充数据
                        table.Cell(row * agrid.Columns.Count + column + 1, 2).Range.Text = (agrid[column, row].Value == null) ? "" : agrid[column, row].Value.ToString();
                    }
                    abar.Value = abar.Value + 1;
                    alb.Text = abar.Value * 100 / abar.Maximum + "%";
                }
                abar.Visible = false;
                abar.Value = 0;

                wordApp.Visible = true;
                //wordApp.UserControl = true;

            }
            catch (Exception theException)
            {
                String errorMessage;
                errorMessage = "Error: ";
                errorMessage = String.Concat(errorMessage, theException.Message);
                errorMessage = String.Concat(errorMessage, " Line: ");
                errorMessage = String.Concat(errorMessage, theException.Source);

                MessageBox.Show(errorMessage, "Error");
            }
        }
    }
}
